<template>
  <div class="userPager">
    <div class="userHeader"></div>
    <div class="content-wrap">
      <p class="content-title" v-if="userState == 0">登录</p>
      <p class="content-title" v-if="userState == 1">注册</p>
      <div class="login" v-if="userState == 0">
        <el-form
          ref="loginForm"
          :model="ruleForm"
          :rules="rules"
          label-width="120px"
          class="demo-ruleForm"
          :label-position="'left'"
        >
          <el-form-item label="请输入用户名:" prop="name">
            <el-input
              v-model="ruleForm.name"
              type="text"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="请输入密码:" prop="password">
            <el-input
              v-model="ruleForm.password"
              type="password"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('loginForm')"
              >登录</el-button
            >
            <el-button @click="resetForm('loginForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="regester" v-if="userState == 1">
        <el-form
          ref="regesterForm"
          :model="ruleForm"
          :rules="rules"
          label-width="120px"
          class="demo-ruleForm"
          :label-position="'left'"
        >
          <el-form-item label="请输入用户名:" prop="name">
            <el-input
              v-model="ruleForm.name"
              type="text"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="请输入密码:" prop="password">
            <el-input
              v-model="ruleForm.password"
              type="password"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码:" prop="checkPass">
            <el-input
              v-model="ruleForm.checkPass"
              type="password"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('regesterForm')"
              >注册</el-button
            >
            <el-button @click="resetForm('regesterForm')">重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="userInfo"></div>
    </div>
    <div class="userFooter"></div>
  </div>
</template>
<style scoped>
.userFooter,
.userHeader {
  background: #409eff;
  width: 100%;
  height: 48px;
}
.userPager {
  position: relative;
  height: 100%;
}
.userFooter {
  position: absolute;
  bottom: 0;
}
.regester,
.login {
  border: 1px solid #409eff;
  padding: 20px;
  border-radius: 8px;
  padding-top: 40px;
  background-color: rgb(236, 245, 255, 0.8);
}
.regester {
  border: 1px solid #67c23a;
}
.content-title {
  margin-top: 20%;
  font-size: 24px;
  font-weight: 600;
  color: #409eff;
}
</style>
<script>
export default {
  name: "userPager",
  props: {
    userState: Number,
  },
  data() {
    const checkIsEmpty = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请填写相关信息"));
      } else {
        callback();
      }
    };
    const checkPss = (rule, value, callback) => {
      if (value != this.ruleForm.password) {
        console.log(value, this.ruleForm);
        callback(new Error("密码不一致！"));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        password: "",
        checkPass: "",
        name: "",
      },
      rules: {
        name: [{ validator: checkIsEmpty, trigger: "blur" }],
        password: [{ validator: checkIsEmpty, trigger: "blur" }],
        checkPass: [{ validator: checkPss, trigger: "blur" }],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let postData = {
            userName: this.ruleForm.name,
            password: this.ruleForm.password,
            passwordBack: this.ruleForm.checkPass,
          };
          if (this.userState == 0) {
            console.log(this.$api);
            this.$api.user.login(postData).then((res) => {
              if (res.status == 0) {
                this.$tip({
                  message: res.msg,
                  type: "success",
                });
                let obj = Object.assign({token:res.data},this.ruleForm);
                this.$emit("loginResFnc", this.ruleForm);
                this.$store.commit('setUserInfo',obj);
                localStorage.setItem("token",res.data);
              } else {
                this.$tip.error(res.msg);
              }
            });
          } else if (this.userState == 1) {
            this.$api.user.register(postData).then((res) => {
              if (res.status == 0) {
                this.$tip({
                  message: res.msg,
                  type: "success",
                });
                this.$emit("loginResFnc", this.ruleForm);
                localStorage.setItem("token",res.data);
              } else {
                this.$tip.error(res.msg);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
  },
};
</script>
